home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 5.9 KB | 215 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWPrvRAc.cpp
- // Release Version: $ 1.0d11 $
- //
- // Copyright: 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef FWPRVRAC_H
- #include "FWPrvRAc.h"
- #endif
-
- #ifndef FWSTRS_H
- #include "FWStrs.h"
- #endif
-
- #ifndef FWFILESP_H
- #include "FWFileSp.h"
- #endif
-
- #ifndef FWEXCDEF_H
- #include "FWExcDef.h"
- #endif
-
- #ifndef FWPRIDEB_H
- #include "FWPriDeb.h"
- #endif
-
- #ifndef FWMEMMGR_H
- #include "FWMemMgr.h"
- #endif
-
- //----------------------------------------------------------------------------------------
- // Mac-only
- //----------------------------------------------------------------------------------------
- #ifdef FW_BUILD_MAC
-
- #pragma segment ResourceFile
-
- #ifndef __ERRORS__
- #include <errors.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __MENUS__
- #include <Menus.h>
- #endif
-
- #ifndef __STRING__
- #include <string.h>
- #endif
-
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
-
- #endif
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export on
- #endif
-
- //========================================================================================
- // CLASS FW_PPrivResourceAccess
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_PPrivResourceAccess::FW_PPrivResourceAccess
- //----------------------------------------------------------------------------------------
-
- FW_PPrivResourceAccess::FW_PPrivResourceAccess(FW_CPrivResourceRep* privResourceAccessRep) :
- FW_CCountedPtr(privResourceAccessRep)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PPrivResourceAccess::~FW_PPrivResourceAccess
- //----------------------------------------------------------------------------------------
-
- FW_PPrivResourceAccess::~FW_PPrivResourceAccess()
- {
- }
-
- //========================================================================================
- // CLASS FW_CPrivResourceRep
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivResourceRep::FW_CPrivResourceRep
- //----------------------------------------------------------------------------------------
-
- FW_CPrivResourceRep::FW_CPrivResourceRep(const FW_CResourceFile& file,
- FW_ResourceId resourceId,
- FW_ResourceType resourceType) :
- FW_CCountedPtrRep(),
- fResourceFile(file),
- fResourceID(resourceId),
- fResourceType(resourceType),
- fResourceHandle(0)
- {
- #ifdef FW_BUILD_MAC
- fLockCount = 0;
- fResourceData = 0;
- {
- FW_CMacResLoadFalse dontLoadResource;
- fResourceHandle = file.GetResourceHandle(resourceId, resourceType);
- }
- #elif defined FW_BUILD_WIN
- fResourceHandle = file.GetResourceHandle(resourceId, resourceType);
- #endif
-
- fResourceSize = GetResourceSize(fResourceHandle);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivResourceRep::~FW_CPrivResourceRep
- //----------------------------------------------------------------------------------------
-
- FW_CPrivResourceRep::~FW_CPrivResourceRep()
- {
- #ifdef FW_BUILD_MAC
- FW_ASSERT(fLockCount == 0);
- if (fResourceData != 0)
- FW_CMemoryManager::FreeSystemHandle(fResourceData);
- #endif
- fResourceFile.ReleaseResourceHandle(fResourceHandle);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivResourceRep::GetResourceSize
- //----------------------------------------------------------------------------------------
-
- unsigned long FW_CPrivResourceRep::GetResourceSize(FW_ResourceHandle handle)
- {
- unsigned long resourceSize;
- #if defined FW_BUILD_MAC
- resourceSize = ::GetResourceSizeOnDisk(handle);
- #elif defined FW_BUILD_WIN
- resourceSize = ::SizeofResource(fResourceFile.PrivGetResourceFileID(), handle);
- #endif
- return resourceSize;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivResourceRep::GetData
- //----------------------------------------------------------------------------------------
-
- void* FW_CPrivResourceRep::GetData()
- {
- void *result;
- FW_ASSERT(fResourceHandle != 0);
- #if defined FW_BUILD_MAC
- FW_ASSERT(fLockCount >= 0);
- if (fLockCount == 0)
- {
- if (fResourceData == 0)
- {
- // ODNewHandle throws kODErrOutOfMemory if it fails
- fResourceData = FW_CMemoryManager::AllocateSystemHandle(fResourceSize);
-
- // Lock the handle, read the resource data
- FW_CMemoryManager::LockSystemHandle(fResourceData);
- ::ReadPartialResource(fResourceHandle, 0, *fResourceData, fResourceSize);
-
- // Throw an exception if the read failed.
- FW_FailOnError(::ResError());
- }
- else
- {
- FW_CMemoryManager::LockSystemHandle(fResourceData);
- }
- }
- ++fLockCount;
- FW_ASSERT(*fResourceData != 0);
- result = *fResourceData;
- #elif defined FW_BUILD_WIN
- fLoadedHandle = ::LoadResource(fResourceFile.PrivGetResourceFileID(), fResourceHandle);
- result = ::LockResource(fLoadedHandle);
- #endif
-
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivResourceRep::ReleaseData
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivResourceRep::ReleaseData()
- {
- #if defined FW_BUILD_MAC
- FW_ASSERT(fLockCount > 0);
- if (--fLockCount == 0)
- {
- FW_CMemoryManager::UnlockSystemHandle(fResourceData);
- // (JEL) ??? Should we HPurge too? My feeling is, tell developers they should
- // generally set all resources to purgeable, but allow them to set a resource
- // nonpurgeable for performance reasons. If we always did an HPurge, we take
- // control from the developer, for no good reason.
- }
- #elif defined FW_BUILD_WIN16
- ::UnlockResource(fLoadedHandle);
- #endif
- }
-
-